﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Esri.ArcGISRuntime.Symbology;
using System.Windows;
using System.Windows.Automation.Peers;
using WRJClient.Model;
using WRJClient.ViewModel;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.UI;
using WRJClient.Common;

namespace WRJClient.UI.Map
{
    public static class GraphicFactory
    {
        public static void CreateCycleGraphic(BaseGraphic graphic, string dbId, GraphicPart part, FillSymbolInfo symbolInfo, double x, double y, double radius)
        {
            if (graphic == null)
            {
                return;
            }
            try
            {
                graphic.Symbol = new SimpleFillSymbol { Color = symbolInfo.FillColor, Outline = new SimpleLineSymbol { Color = symbolInfo.BorderColor, Width = symbolInfo.BorderWidth, Style = symbolInfo.BorderStyle, AntiAlias = true }, Style = symbolInfo.FillStyle };
                MapPoint center = new MapPoint(x, y, SpatialReferences.Wgs84);
                PointCollection pointCollection = ArcCommon.GetCyclePoints(center, radius);
                graphic.Geometry = new Polygon(pointCollection);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        public static void CreatePolygonGraphic(BaseGraphic graphic, string dbId, GraphicPart part, FillSymbolInfo symbolInfo, string path)
        {
            if (graphic == null)
            {
                return;
            }
            try
            {
                graphic.Symbol = new SimpleFillSymbol { Color = symbolInfo.FillColor, Outline = new SimpleLineSymbol { Color = symbolInfo.BorderColor, Width = symbolInfo.BorderWidth, Style = symbolInfo.BorderStyle, AntiAlias = true }, Style = symbolInfo.FillStyle };
                PointCollection pointCollection = ArcCommon.PointCollectionFromGeoPointJson(path);
                graphic.Geometry = new Polygon(pointCollection);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        public static void CreateLineGraphic(BaseGraphic graphic, string dbId, GraphicPart part, LineSymbolInfo symbolInfo, PointCollection pointCollection)
        {
            if (graphic == null)
            {
                return;
            }
            try
            {
                graphic.Symbol = new SimpleLineSymbol { Color = symbolInfo.BorderColor, Width = symbolInfo.BorderWidth, Style = symbolInfo.BorderStyle, AntiAlias = true };
                graphic.Geometry = new Polyline(pointCollection);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public static void CreateImageGraphic(BaseGraphic graphic, string dbId, GraphicPart part, PictureMarkerSymbolInfo symbolInfo, double x, double y, double angle = 0)
        {
            if (graphic == null)
            {
                return;
            }
            try
            {
                graphic.Symbol = new PictureMarkerSymbol(RuntimeImageHelper.Instance.GetRuntimeImage(symbolInfo.FileName)) { OffsetX = symbolInfo.OffsetX, OffsetY = symbolInfo.OffsetY, Angle = angle, AngleAlignment = SymbolAngleAlignment.Map };
                graphic.Geometry = new MapPoint((double)x, (double)y, SpatialReferences.Wgs84);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        public static void CreateTextGraphic(BaseGraphic graphic, string dbId, GraphicPart part, TextSymbolInfo symbolInfo, string text, double x, double y)
        {
            if (graphic == null)
            {
                return;
            }
            try
            {
                graphic.Symbol = new TextSymbol { AngleAlignment = SymbolAngleAlignment.Map, Text = text, Size = symbolInfo.Size, Color = symbolInfo.Color, FontWeight = symbolInfo.FontWeight, HaloColor = symbolInfo.HaloColor, HaloWidth = symbolInfo.HaloWidth, OffsetX = symbolInfo.OffsetX, OffsetY = symbolInfo.OffsetY, HorizontalAlignment = Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Left, VerticalAlignment = Esri.ArcGISRuntime.Symbology.VerticalAlignment.Top };
                graphic.Geometry = new MapPoint(x, y, SpatialReferences.Wgs84);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
    }
}
